package com.tqjc.common.ScheduledTask;

import cn.hutool.core.collection.CollectionUtil;
import com.tqjc.common.consts.NormalConst;
import com.tqjc.execute.utils.HttpPLCUtils;
import com.tqjc.mes.entity.SteelInfo;
import com.tqjc.mes.entity.Test;
import com.tqjc.mes.service.SteelInfoService;
import com.tqjc.mes.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 86195
 * @CreateTime: 2024-06-04  11:07
 * @Description: TODO
 * @Version: 1.0
 */
@Component
@Slf4j
public class SteelNoListenTask {

    private Map<String,String> STEEL_NO_MAP = new HashMap<>();

    @Autowired
    SteelInfoService steelInfoService;

    @Value("${server_host}")
    private String serverHost;

    @Autowired
    private RestTemplate restTemplate;

    private Boolean SteelNoFlag;

    @Resource
    TestService testService;

    private static Map<String,String> allowSignalMap = new HashMap(2);

    static {
        allowSignalMap.put("11C北","DJ2允许起吊");
        allowSignalMap.put("11C南","DJ3允许起吊");
    }

    @Scheduled(cron = "0/5 * *  * * ? ")
    public void listenSteelNo() {
        for (String crane : NormalConst.DJ_CRANE_LIST) {

            try {

                String allowSignal = allowSignalMap.get(crane);
                //1.辊道运行起吊信号出来，运行起吊
                String signalRes = HttpPLCUtils.plcReadbc(restTemplate, serverHost, crane, allowSignal, true);

                String mode = HttpPLCUtils.plcReadbc(restTemplate,serverHost,crane,"模式",false);
                if(!"5".equals(mode)){

                    if("true".equalsIgnoreCase(signalRes)){

                        //2.获取一级辊道上的钢板信息
                        String steelNo = HttpPLCUtils.plcReadbc(restTemplate,serverHost,crane,"获取钢板号",true); //获取钢板号
                        log.info("监听钢板信息任务中，天车【{}】从辊道读取的钢板号为：{}",crane,steelNo);

                        if(!"0".equals(steelNo)&& StringUtils.isNotEmpty(steelNo)&&steelNo.length()>=7){
                            //获取上一次钢板号记录
                            log.info("监听钢板信息任务中，对应钢板号的长度为{}",steelNo.length());
                            String lastSteelNo = STEEL_NO_MAP.get(crane);
                            if(!steelNo.equals(lastSteelNo)){
                                //上次钢板号与本次钢板号不一致
                                //3.与之前钢板号进行比较，不同则需要从MES库中获取钢板尺寸信息
                                if(steelNo.length()==7){
                                    steelNo="0"+steelNo;
                                }
                                if(steelNo.length()==8){
                                    steelNo = steelNo.substring(0,5)+"100";
                                }
                                List<SteelInfo> steels= steelInfoService.selectByMatNo(steelNo);
                                //List<SteelInfo> steels = testService.mockData(steelNo);

                                if(CollectionUtil.isNotEmpty(steels)){
                                    SteelInfo steelInfo = steels.get(0);
                                    sendMsgToPLC(crane,"false");
                                    log.info("监听钢板信息任务中，天车【{}】,钢板号为：{}，从MES数据库查出来钢板信息为：{}",crane,steelNo,steelInfo);

                                    Float matActWidth = steelInfo.getMatActWidth();
                                    if(matActWidth >= 3000){
                                        //判断当前动作是否完成
                                        String flag = HttpPLCUtils.plcReadbc(restTemplate, serverHost, crane, "手动放板任务完成", false);
                                        //当前动作完成
                                        if(flag.equalsIgnoreCase("true")){
                                            //认为为超宽的钢板   >= 3000
                                            //4.写入“超宽板选择点位”
                                            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,crane,"DB41.0.0",
                                                    HttpPLCUtils.dataTypeList.get("bool"),"true","超宽板选择",false);
                                            //5.更新map数据
                                            STEEL_NO_MAP.put(crane,steelNo);
                                        }


                                    }
                                } else {
                                    //未获取到钢板信息
                                    sendMsgToPLC(crane,"true");
                                }
                            }
                        } else{
                            //未获取到钢板信息
                            sendMsgToPLC(crane,"true");
                        }
                    }
                }


            } catch (Exception e){
                log.error("监听钢板信息出现异常",e);
            }
        }
    }

    //发送信息至PLC
    private void sendMsgToPLC(String crane,String value){
        Boolean currFlag = Boolean.valueOf(value);
        if (SteelNoFlag == null || SteelNoFlag != currFlag) {
            SteelNoFlag = currFlag;
            log.info("天车：{}获取钢板信息出现异常",crane);
            try {
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,crane,"DB20.293.5",
                        HttpPLCUtils.dataTypeList.get("bool"),value,"钢板信息异常",false);
            } catch (Exception e){
                log.error("监听钢板信息中，发送未获取到钢板信息至PLC,出现异常",e);

            }
        }

    }

}
